package com.fuxiong.controller;

import com.fuxiong.pojo.User;
import com.fuxiong.pojo.query.UserQuery;
import com.fuxiong.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/")
    public String index(Model model, UserQuery userQuery){
        PageInfo<User> userPageInfo = userService.getUserByName(userQuery);
        model.addAttribute("page",userPageInfo);
        return "index";

    }

    @PostMapping("/")
    public String listUserByName(Model model, UserQuery userQuery){
        PageInfo<User> userPageInfo = userService.getUserByName(userQuery);
        model.addAttribute("page",userPageInfo);
        return "index";
    }

    //根据id删除用户
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id, RedirectAttributes attributes){
        boolean b = userService.deleteUserById(id);
        if (b) {
            attributes.addFlashAttribute("message","删除成功");
            return "redirect:/";
        }else {
            attributes.addFlashAttribute("message","删除失败");
            return "redirect:/";
        }

    }

    //修改用户信息
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Integer id,Model model){

        model.addAttribute("user",userService.queryUserById(id));
        return "editUser";
    }

    //编辑用户
    @PostMapping("/edit")
    public String edit(User user, RedirectAttributes attributes,UserQuery userQuery){
        Integer id = user.getId();
        //如果已经存在用户，则提示
        user.setUsername(user.getUsername());
        PageInfo<User> userPageInfo = userService.getUserByName(userQuery);
        if (id != null) {
            if (userPageInfo.getSize() == 0) {
                boolean b = userService.updateUser(user);
                if (b) {
                    attributes.addFlashAttribute("message","修改成功");
                    return "redirect:/";
                }else {
                    attributes.addFlashAttribute("message","修改失败");
                    return "redirect:/";
                }
            }else {
                attributes.addFlashAttribute("message","该用户已经存在");
                return "redirect:/edit/"+user.getId();
            }
        }else {
            if (userPageInfo.getSize() == 0) {
                boolean b = userService.addUser(user);
                if (b) {
                    attributes.addFlashAttribute("message","新增成功");
                    return "redirect:/";
                }else {
                    attributes.addFlashAttribute("message","新增失败");
                    return "redirect:/";
                }
            }else {
                attributes.addFlashAttribute("message","该用户已经存在");
                return "redirect:/edit/"+user.getId();
            }
        }

    }

    @GetMapping("/addUser")
    public String addUser(Model model){
        User user = new User();
        model.addAttribute("user",user);
        return "editUser";
    }


}
